from django.contrib import admin

from .models import Classify,Content,Detail,Modification
# Register your models here.
class ContentInline(admin.TabularInline):
    model = Content
    extra = 0

class DetailInline(admin.TabularInline):
    model = Detail
    extra = 0

class ClassifyAdmin(admin.ModelAdmin):
    list_display = ('name', 'having')
    inlines = [DetailInline]

class DetailAdmin(admin.ModelAdmin):
    list_display = ('name', 'belong')
    inlines = [ContentInline]

class ContentAdmin(admin.ModelAdmin):
    list_display = ('user', 'time', 'title', 'text','belong', 'status')
    # list_editable = ['status']
    list_filter = ['user', 'time']
    ordering = ('status','time')
    search_fields = ['user', 'time', 'title']

    def add(self, request, queryset):
        rows_updated = queryset.update(status=1)
        if rows_updated == 1:
            message_bit = "1 story was"
        else:
            message_bit = "%s stories were" % rows_updated
        self.message_user(request, "%s successfully marked as published." % message_bit)


    add.short_description = "Approval selected contents"
    actions = [add]

class modificationAdmin(admin.ModelAdmin):
    def disagree(self, request, queryset):
        count = queryset.update(check = 0, status = 1)
        self.message_user(request, "%s successfully marked as disagreement." % count)
    def agree(self, request, queryset):
        # queryset.update(status=1)
        count = 0
        for item in queryset:
            id = item.Cid
            text = item.text
            content = Content.objects.get(id=id)
            content.text = text
            item.check = 1
            item.status = 1
            count+=1
            item.save()
            content.save()
        self.message_user(request, "%s successfully marked as published." % count)
    agree.short_description = "Agree selected contents"
    disagree.short_description = "Disagree selected contents"
    list_display = ('Cid','text', 'check', 'Status')
    actions = [agree, disagree]
    # ordering = ('status')



admin.site.register(Classify, ClassifyAdmin)
admin.site.register(Content,ContentAdmin)
admin.site.register(Detail, DetailAdmin)
admin.site.register(Modification, modificationAdmin)
